package com.aaa.mbs.controller;

import com.aaa.common.constants.ReturnStatus;
import com.aaa.common.util.Result;
import com.aaa.mbs.entity.Member;
import com.aaa.mbs.service.MemberService;
import com.aaa.mbs.util.SmsUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;


/**
 * (Member)表控制层
 *
 * @author makejava
 * @since 2020-12-04 20:52:31
 */
@RestController
@RequestMapping("member")
public class MemberController {
    /**
     * 服务对象
     */
    @Resource
    private MemberService memberService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private HttpServletRequest request;

    /**
     * 根据账号查询
     * @param userName
     * @return
     */
    @GetMapping("queryByUserName")
    public Member queryByUserName(@RequestParam("userName") String userName) {
        return memberService.queryByUserName(userName);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    @ApiOperation("通过主键查询单条数据")
    public Member selectOne(Integer id) {
        return this.memberService.queryById (id);
    }

    /**
     * 查询所有分页加条件查询
     *
     * @return Result
     */
    @PostMapping("queryAll")
    @ApiOperation("查询所有分页加条件查询")
    public Result queryAll(@RequestBody(required = false) Member member) {
        return this.memberService.queryAll (member);
    }



    @PostMapping("queryAllMap")
    @ApiOperation("联合加条件查询")
    public Result queryAllMap(@RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize, @RequestBody Map map) {
        return this.memberService.queryAllMap (pageNo, pageSize,map);
    }

    /**
     * 根据ID删除
     *
     * @param id id
     * @return Result
     */
    @DeleteMapping("deleteById")
    @ApiOperation("根据ID删除")
    public Result updateById(Integer id) {
        return new Result (ReturnStatus.SUCCESS.getReturnCode (),
                ReturnStatus.SUCCESS.getReturnMsg (), this.memberService.deleteById (id));
    }

    /**
     * 更新
     *
     * @param @param member 实例对象
     * @return Result
     */
    @PostMapping("update")
    @ApiOperation("更新")
    public Result update(@ApiParam("更新json数据") @RequestBody Member member) {
        return new Result (ReturnStatus.SUCCESS.getReturnCode (),
                ReturnStatus.SUCCESS.getReturnMsg (), this.memberService.update (member));
    }

    /**
     * 添加
     *
     * @param @param member 实例对象
     * @return Result
     */
    @PostMapping("insert")
    @ApiOperation("添加")
    public Result insert(@ApiParam("添加json数据") @RequestBody Member member) {
        return this.memberService.insert (member);
    }

    /**
     * 发送验证码
     * @param phoneNum
     * @return
     */
    @GetMapping("sentSM")
    @ApiOperation("发送验证码")
    public Result sentSM(String phoneNum){
        Random random=new Random();
        String verifyCode=random.nextInt(100000)+100000+"";
        //存储验证码到redis
        redisTemplate.opsForValue().set(phoneNum,verifyCode,10, TimeUnit.MINUTES);
        return new Result (ReturnStatus.SUCCESS.getReturnCode (),
                ReturnStatus.SUCCESS.getReturnMsg (),
                SmsUtil.sendSM(phoneNum,verifyCode));
    }

    /**
     * 微信小程序实现微信登录
     * @param code
     * @param APPID
     * @param APPSecret
     * @return
     */
    @GetMapping("getWxUserOpenid")
    @ApiOperation("微信小程序实现微信登录")
    public Map getWxUserOpenid(String code, String APPID, String APPSecret){
        return this.memberService.getWxUserOpenid(code,APPID,APPSecret);
    }

    @GetMapping("getAddr")
    @ApiOperation("获取ip")
    public Object getAddr(){
        String ip = request.getHeader("X-Forwarded-For");
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            // 多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = ip.indexOf(',');
            if (index != -1) {
                ip = ip.substring(0, index);
            }
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_CLUSTER_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_FORWARDED");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_VIA");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("REMOTE_ADDR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            if (ip.equals("127.0.0.1") || ip.equals("0:0:0:0:0:0:0:1")) {
                //根据网卡取本机配置的IP
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                ip = inet.getHostAddress();
            }
        }
        System.out.println(ip);
        Object o = redisTemplate.opsForValue().get(ip);
        if(o==null){
            Random random = new Random(3);
            int i = random.nextInt();
            System.out.println();
            redisTemplate.opsForValue().set(ip,"图片"+i);
            return "jsoup"+ip+redisTemplate.opsForValue().get(ip);
        }else{
            return "redis"+o+ip;
        }
//        return ip;
    }

    public static void main(String[] args) {

    }
}